package com.jialanmo.zhihudaily;

import android.content.Context;

import com.google.gson.Gson;
import com.jialanmo.zhihudaily.data.ZhihuApi;

import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

public final class Application extends android.app.Application {

    private ZhihuApi mZhihuApi;
    private RxBus mRxBus;

    @Override
    public void onCreate() {
        super.onCreate();
        OkHttpClient okHttpClient = new OkHttpClient.Builder()
                .build();
        Gson gson = new Gson();
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://news-at.zhihu.com/api/4/")
                .client(okHttpClient)
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .addConverterFactory(GsonConverterFactory.create(gson))
                .build();
        mZhihuApi = retrofit.create(ZhihuApi.class);
        mRxBus = new RxBus();
    }

    public ZhihuApi getZhihuApi() {
        return mZhihuApi;
    }

    public RxBus getRxBus() {
        return mRxBus;
    }

    public static Application getApplication(Context context) {
        return (Application) context.getApplicationContext();
    }
}
